#include "StdAfx.h"
#include "TimeController.h"


CTimeController::CTimeController(void)
{
}


CTimeController::~CTimeController(void)
{
}

CVector3D CTimeController::EvaluatePosition(DATA_TYPE fTime) const
{
	if (m_vKeyFrames.size() == 0)
	{
		return CVector3D();
	}

	if (fTime <= m_vKeyFrames[0].fTime)
	{
		return m_vKeyFrames[0].vPosition;
	}

	for (unsigned int i = 1; i < m_vKeyFrames.size(); ++i)
	{
		if (fTime <= m_vKeyFrames[i].fTime)
		{
			DATA_TYPE t = (fTime - m_vKeyFrames[i - 1].fTime) / (m_vKeyFrames[i].fTime - m_vKeyFrames[i - 1].fTime);

			return (1.f - t) * m_vKeyFrames[i - 1].vPosition + t * m_vKeyFrames[i].vPosition;
		}
	}

	return m_vKeyFrames.back().vPosition;
}

void CTimeController::AddKey(DATA_TYPE fTime, const CVector3D& vPosition)
{
	m_vKeyFrames.push_back(KeyFrame(fTime, vPosition));
}